﻿using Ext.Net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CRM_Manager_ERP_SupplyEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string objid = ViewState_ObjID = string.IsNullOrEmpty(Request.QueryString["ObjId"]) ? "Add" : Request.QueryString["ObjId"];
            ShowEditInfo(objid);
        }
    }

    private void ShowEditInfo(string objid)
    {
        if (objid != "Add")
        {
            DataTable dt = CRMMis.BLL.ERP_Supply.GetList(" SupplyID=" + objid).Tables[0];
            SetContainer(dataForm, dt);
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                int planState = Convert.ToInt32(row["CheckState"]);
                //根据状态控制
                if (planState != 1)
                    extBtn_CheckInfo.Hidden = true;
                if (planState > 0)
                {
                    extBtn_SubmitInfo.Hidden = true;
                    extBtn_Save.Hidden = true;
                    SetContainerReadOnly(dataForm, true);
                }
                if (planState > 1)
                    extBtn_CheckInfo.Hidden = true;
                if (BS_LoginRoles.Contains("02")&& planState==2)
                {
                    extBtn_CheckInfo.Hidden = false;
                    SetContainerReadOnly(dataForm, false);
                }
            }
        }
    }


    [DirectMethod]
    public void SaveData(int type)
    {
        try
        {
            CRMMis.Model.ERP_Supply model;
            if (ViewState_ObjID == "Add")
            {
                model = new CRMMis.Model.ERP_Supply();
                model.SupplyID = CRMMis.BLL.ERP_Supply.GetNewID();
                model.InDate = DateTime.Now.ToString();
                model.InUser = BS_LoginUserID;
                model.CheckState = 0;
                if(type == 1)
                    model.CheckState = 1;   //提交
            }
            else
            {
                model = CRMMis.BLL.ERP_Supply.GetModel(Convert.ToInt32(ViewState_ObjID));
                if (type == 1)
                {
                    if (model.CheckState == 0)
                        model.CheckState = 1;    //提交
                    else
                    {
                        X.Msg.Alert("系统提示", "提交失败！当前记录已经提交").Show();
                        return;
                    }
                }
                else if (type == 2)
                {
                    if (model.CheckState == 1)
                        model.CheckState = 2;    //审核
                    else
                    {
                        if (BS_LoginRoles.Contains("02") && model.CheckState == 2)
                        {
                            model.SupplyName = exttxtf_SupplyName.Text.Trim();
                            model.Spell = exttxtf_Spell.Text.Trim();
                            model.Email = exttxtf_Email.Text.Trim();
                            model.Linkman = exttxtf_Linkman.Text.Trim();
                            model.Telephone = exttxtf_Telephone.Text.Trim();
                            model.Address = exttxtf_Address.Text.Trim();
                            model.Remark = exttxtf_Remark.Text.Trim();
                        }
                        else
                        {
                            X.Msg.Alert("系统提示", "审核失败！当前记录已经审核").Show();
                            return;
                        }
                    }
                }

            }
            if (type < 2)
            {
                model.SupplyName = exttxtf_SupplyName.Text.Trim();
                model.Spell = exttxtf_Spell.Text.Trim();
                model.Email = exttxtf_Email.Text.Trim();
                model.Linkman = exttxtf_Linkman.Text.Trim();
                model.Telephone = exttxtf_Telephone.Text.Trim();
                model.Address = exttxtf_Address.Text.Trim();
                model.Remark = exttxtf_Remark.Text.Trim();
            }
            if (ViewState_ObjID == "Add")
            {
                int c = CRMMis.BLL.ERP_Supply.GetRecorderCount("SupplyName='" + model.SupplyName + "'");
                if (c > 0)
                {
                    X.Msg.Alert("系统提示", "保存失败！</br>"+ exttxtf_SupplyName.FieldLabel + "不能重复").Show();
                    exttxtf_SupplyName.Focus();
                    return;
                }
                CRMMis.BLL.ERP_Supply.Add(model);
                ViewState_ObjID = model.SupplyID.ToString();
            }
            else
            {
                int c = CRMMis.BLL.ERP_Supply.GetRecorderCount("SupplyName='" + model.SupplyName + "' and SupplyID<>"+ ViewState_ObjID);
                if (c > 0)
                {
                    X.Msg.Alert("系统提示", "保存失败！</br>" + exttxtf_SupplyName.FieldLabel + "不能重复").Show();
                    exttxtf_SupplyName.Focus();
                    return;
                }
                CRMMis.BLL.ERP_Supply.Update(model);

            }
            string handler = "";
            if (type > 0)
                handler = "closeWin();";
            X.Msg.Alert("系统提示", "保存成功！", handler).Show();
            return;
        }
        catch (Exception e)
        {
            X.Msg.Alert("系统提示", "保存出错！</br>" + e.Message).Show();
        }
        return;
    }

}